package com.ja.openchat.views;

import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.group.GroupMemberInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class BottomAddGroupDialog extends BottomPopupView{

    private final ChatInfo chat;
    private final OnItemClickListner listner;
    private FragmentActivity context;
    private UserAvatarView avatarView;
    private TextView headerName;
    private TextView tvMenberNum;
    private TextView tvAddGroup;
    private TextView tvCancelAddGroup;
    private LinearLayout menberLayout;
    private UserAvatarTextView avatarViewTxt;

    public BottomAddGroupDialog(@NonNull FragmentActivity context, ChatInfo chat, OnItemClickListner listner) {
        super(context);
        this.chat = chat;
        this.context = context;
        this.listner = listner;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.msg_bottom_addgroup_view;
    }


    @Override
    protected void onCreate() {
        super.onCreate();
        avatarView = findViewById(R.id.user_image);
        headerName = findViewById(R.id.user_name);
        tvMenberNum = findViewById(R.id.tv_menber_num);
        tvAddGroup = findViewById(R.id.tv_add_group);
        tvCancelAddGroup = findViewById(R.id.tv_cancel_add_group);
        menberLayout = findViewById(R.id.menber_layout);


        TelegramManager.getTelegramManager().getSuperGroupMembers(context,chat.id, chat.superGroupId(), null, null, 0, 1000, new TgResultBlock<List<GroupMemberInfo>>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, List<GroupMemberInfo> obj) {
                if (obj != null && obj.size() > 0) {
                    tvMenberNum.setText(obj.size()+"位成员");
                    for (int i=0;i<obj.size();i++){
                        GroupMemberInfo member = obj.get(i);
                        if(i<3){
                            UserInfo user = TelegramManager.getTelegramManager().getTgData().contactInfo(member.user_id);
                            if (user != null) {
                                avatarView.showUser(user);
                            } else {
                                avatarView.showName(UserInfo.userDisplayName(member.user_id));
                            }
                            View textView = inflate(context,R.layout.item_view_avatar_text,null);
                            avatarViewTxt = textView.findViewById(R.id.user_avatar);
                            avatarViewTxt.showUser(user);
                            menberLayout.addView(textView);
                        }else{
                            break;
                        }
                    }

                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {

            }
        });


        if (chat != null) {
            if (chat.isGroup()) {//群组
                //群组名称
                headerName.setText(chat.title);
                //群组头像
                avatarView.showGroup(chat);
            } else {//单聊
                //对方昵称
                headerName.setText(UserInfo.userDisplayName(chat.userId()));
                //
                UserInfo user = TelegramManager.getTelegramManager().getTgData().contactInfo(chat.userId());
                if (user != null) {
                    avatarView.showUser(user);
                } else {
                    avatarView.showName(UserInfo.userDisplayName(chat.userId()));
                }
            }

            tvAddGroup.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner != null) {
                        listner.clickPostion(0,chat);
                    }
                    dismiss();
                }
            });
            tvCancelAddGroup.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listner != null) {
                        listner.clickPostion(1,chat);
                    }
                    dismiss();
                }
            });
        }
    }


    /**
     * @param url 通过URL得到 Drawable
     * @return
     */
    public void getDrawableGlide(String url, CustomTarget<Drawable> customTarget) {
        Glide.with(context).load(url).into(customTarget);
    }

    /**
     * 这是一个耗时的操作需要异步处理
     *
     * @param url 通过URL得到 Drawable
     * @return
     */
    public  Drawable getDrawableGlide(String url) {
        try {
            return Glide.with(context)
                    .load(url)
                    .submit()
                    .get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext()) * 1f);
    }


    public interface OnItemClickListner {
        void clickPostion(int position,ChatInfo postion);
    }

}
